$(function () {
    let layer = layui.layer;

    let q = {
        page: '',
        perpage: 10
    }
    initComment(q);
    // 页面渲染
    function initComment(q) {
        $.ajax({
            url: '/admin/comment/search',
            type: 'get',
            data: q,
            success: (res) => {
                // console.log(res.data);

                let htmlStr = template("tpl-table", res.data)
                $("tbody").html(htmlStr)
                renderPage(res.data.totalCount)
            }

        })
    }

    // 分页
    let laypage = layui.laypage;

    function renderPage(total) {
        laypage.render({
            elem: 'pageBox',
            count: total,
            limit: q.perpage,
            curr: q.page,

            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            limits: [5, 10, 20, 30],
            jump: function (obj, first) {
                // console.log(obj, first);
                q.page = obj.curr;
                q.perpage = obj.limit
                // q.pagenum = ;

                if (!first) {
                    initComment(q);
                }
            }
        })
    }

    // 审核通过
    $("tbody").on("click", ".btn-pass", function () {
        let id = $(this).attr("data-id")
        $.ajax({
            url: '/admin/comment/pass',
            type: 'post',
            data: {
                id: id
            },
            success: (res) => {
                console.log(res);
                if (res.code !== 200) {
                    return layer.msg(res.msg, {
                        icon: 5
                    })
                }
                layer.msg("恭喜您,设置成功!", {
                    icon: 6
                })
                initComment(q);
            }
        })
    });

    // 审核不通过
    $("tbody").on("click", ".btn-reject", function () {
        let id = $(this).attr("data-id")
        $.ajax({
            url: '/admin/comment/reject',
            type: 'post',
            data: {
                id: id
            },
            success: (res) => {
                console.log(res);
                if (res.code !== 200) {
                    return layer.msg(res.msg, {
                        icon: 5
                    })
                }
                layer.msg("恭喜您,设置成功!", {
                    icon: 6
                })
                initComment(q);
            }
        })
    });


    //  删除
    $("tbody").on("click", ".btn-delete", function () {
        let id = $(this).attr("data-id")

        $.ajax({
            url: '/admin/comment/delete',
            type: 'post',
            data: {
                id: id
            },
            success: (res) => {
                // console.log(res);
                if (res.code !== 200) {
                    return layer.msg(res.msg, {
                        icon: 5
                    })
                }
                layer.msg("删除成功!", {
                    icon: 6
                })
                initComment(q);
            }
        })
    })
})